package com.guit.hrms.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.guit.hrms.dto.otherDept.AddOtherDeptDTO;
import com.guit.hrms.mapper.EmployeeMapper;
import com.guit.hrms.mapper.OtherDeptMapper;
import com.guit.hrms.mapper.PositionMapper;
import com.guit.hrms.mapper.SettingsMapper;
import com.guit.hrms.po.otherDept.OtherDept;
import com.guit.hrms.po.position.Position;
import com.guit.hrms.service.OtherDeptService;
import com.guit.hrms.vo.otherDept.OtherDeptVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OtherDeptServiceImpl implements OtherDeptService {
    @Autowired
    OtherDeptMapper otherDeptMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private SettingsMapper settingsMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 新增其它部门
     * @param addOtherDeptDTO
     */
    @Override
    public void addOtherDept(AddOtherDeptDTO addOtherDeptDTO) {
        OtherDept otherDept = new OtherDept();

        BeanUtils.copyProperties(addOtherDeptDTO,otherDept);
        otherDept.setCreateTime(LocalDateTime.now());
        otherDept.setUpdateTime(LocalDateTime.now());

        otherDeptMapper.addOtherDept(otherDept);
    }

    /**
     * 查看其它部门
     * @param otherDeptId
     * @return
     */
    @Override
    public List<OtherDeptVO> queryOtherDept(Integer otherDeptId) {
        if (otherDeptId == null) {
            // 查询所有其它部门
            List<OtherDept> allDepts = otherDeptMapper.getAllOtherDepts();
            if (allDepts.isEmpty()) {
                return new ArrayList<>();
            }
            
            // 转换为 VO 列表
            return allDepts.stream().map(dept -> {
                OtherDeptVO vo = new OtherDeptVO();
                BeanUtils.copyProperties(dept, vo);
                List<Position> positions = positionMapper.getPositionByDeptId(dept.getOtherDeptId());
                vo.setPositions(positions != null ? positions : new ArrayList<>());
                return vo;
            }).collect(Collectors.toList());
        } else {
            // 查询单个部门
            OtherDept otherDept = otherDeptMapper.getOtherDeptById(otherDeptId);
            if (otherDept == null) {
                return null;
            }
            
            OtherDeptVO vo = new OtherDeptVO();
            BeanUtils.copyProperties(otherDept, vo);
            List<Position> positions = positionMapper.getPositionByDeptId(otherDeptId);
            vo.setPositions(positions);
            return Collections.singletonList(vo);
        }
    }

    /**
     * 更新其它部门
     * @param otherDept
     */
    @Override
    public void updateOtherDept(OtherDept otherDept) {
        otherDept.setUpdateTime(LocalDateTime.now());
        otherDeptMapper.updateOtherDept(otherDept);
    }

    /**
     * 批量删除其它部门
     * @param deptIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOtherDept(List<Integer> deptIds) {
        try {
            //获取设置
            String setting_name = "部门是否连带删除";
            Integer value = settingsMapper.getValueBySettingName(setting_name);
            
            // 如果设置值为空，默认使用不连带删除的模式
            int deleteMode = (value != null) ? value : 0;
            
            if (deleteMode == 0) {
                // 仅删除指定的部门
                for (Integer deptId : deptIds) {
                    otherDeptMapper.deleteOtherDeptById(deptId);
                }
            } else if (deleteMode == 1) {
                // 连带删除子部门和相关数据
                for (Integer deptId : deptIds) {
                    otherDeptMapper.deleteOtherDeptById(deptId);
                    //删除子部门
                    otherDeptMapper.deleteOtherDeptByParentId(deptId);
                    //删除子岗位
                    positionMapper.deletePositionByDeptId(deptId);
                    //修改员工所属
                    employeeMapper.setDeptAndPositionByDeptId(deptId);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("删除其它部门失败: " + e.getMessage());
        }
    }
}
